package cn.funeralobjects.upms.service.impl;

import cn.funeralobjects.common.jpa.service.JpaDelSaltService;
import cn.funeralobjects.common.jpa.service.JpaEntityService;
import cn.funeralobjects.common.jpa.service.JpaModifyService;
import cn.funeralobjects.common.repository.DelSaltRepository;
import cn.funeralobjects.common.repository.EntityRepository;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.upms.entity.EUser;
import cn.funeralobjects.upms.entity.EUserSecret;
import cn.funeralobjects.upms.model.UserSecret;
import cn.funeralobjects.upms.repository.UserSecretRepository;
import cn.funeralobjects.upms.service.UserSecretService;
import cn.funeralobjects.upms.service.UserService;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;
import cn.funeralobjects.util.annotation.ArgNotZero;
import cn.funeralobjects.util.annotation.AssertArg;
import org.springframework.data.repository.CrudRepository;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/6/3 3:11 PM
 */
@CommonService
public class UserSecretServiceImpl implements UserSecretService,
        JpaEntityService<EUserSecret, Integer>,
        JpaModifyService<UserSecret, EUserSecret, Integer>,
        JpaDelSaltService<EUserSecret, Integer> {
    @Resource
    private UserSecretRepository userSecretRepository;

    @Resource
    private UserService userService;

    @AssertArg
    @Override
    public void checkConflict(@AssertArg UserSecret userSecret) throws DataConflictException {

    }

    @Override
    public EUserSecret toEntity(UserSecret userSecret) {
        EUserSecret entity = new EUserSecret();
        EUser user = new EUser();
        user.setId(userSecret.getUserId());
        entity.setSecretKey(userSecret.getSecretKey())
                .setUser(user)
                .setId(userSecret.getUserId());
        return entity;
    }

    @Override
    public EntityRepository<EUserSecret, Integer> entityRepository() {
        return userSecretRepository;
    }

    @Override
    public CrudRepository<EUserSecret, Integer> modifyRepository() {
        return userSecretRepository;
    }

    @AssertArg
    @Override
    public void checkConflict(@AssertArg UserSecret userSecret, @ArgNotZero Integer withoutId) throws DataConflictException {

    }

    @Override
    public void copyToEntity(UserSecret sourceModify, EUserSecret targetEntity) {
        targetEntity.setSecretKey(sourceModify.getSecretKey());
    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }

    @Override
    public void validateModify(UserSecret userSecret) {
        Assert.argAssert(userSecret, "userSecret");
        userService.mustExistsById(userSecret.getUserId());
    }

    @Override
    public DelSaltRepository<EUserSecret, Integer> delSaltRepository() {
        return userSecretRepository;
    }
}
